package one.stand.service.impl;

import one.stand.model.BusinessCardVideoModel;
import one.stand.model.ResultModel;
import one.stand.request.BusinessCardRequest;
import one.stand.request.BusinessCardVideoRequest;
import one.stand.response.BusinessCardResponse;
import one.stand.response.BusinessCardVideoResponse;
import one.stand.service.BusinessCardVideoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author: QIK
 * @CreateDate: 2019/6/21 16:48
 */
@Service
public class BusinessCardVideoServiceImpl extends PublicBaseService implements BusinessCardVideoService {


    @Override
    public ResultModel<Boolean> addVideo(BusinessCardVideoRequest request) {
        if (StringUtils.isBlank(request.getVideoTitle()) || StringUtils.isBlank(request.getVideoUrl())) {
            return ResultModel.checkParamFail();
        }

        Integer count = businessCardVideoMapper.count(new BusinessCardVideoModel() {{
            setCardId(request.getMyCardId());
        }});
        if (count >= 3) {
            return ResultModel.fail("视频数量达到上线");
        }
        BusinessCardVideoModel inModel = new BusinessCardVideoModel();
        BeanUtils.copyProperties(request, inModel);
        inModel.setCardId(request.getMyCardId());
        inModel.setCreateTime(currentDate());
        int insertRes = businessCardVideoMapper.insertSelective(inModel);
        if (insertRes <= 0) {
            return ResultModel.fail();
        }
        return ResultModel.success();
    }

    @Override
    public ResultModel<Boolean> addVideo(BusinessCardRequest request) {
        List<BusinessCardVideoRequest> videoRequestList=request.getVideoRequestList();
        if(null==videoRequestList || videoRequestList.size()<=0){
            log.info("视频数据为空，不进行插入名片视频操作。。。");
            return ResultModel.success();
        }
        //TODO 需要优化为批量插入
        videoRequestList.forEach(l->{{
            l.setMyCardId(request.getMyCardId());
            addVideo(l);
        }});
        return ResultModel.success();
    }

    @Override
    public ResultModel<Boolean> uodVideo(BusinessCardVideoRequest request) {
        if (StringUtils.isBlank(request.getVideoTitle()) || StringUtils.isBlank(request.getVideoUrl()) || null == request.getVideoId()) {
            return ResultModel.checkParamFail();
        }
        BusinessCardVideoModel inModel = new BusinessCardVideoModel();
        BeanUtils.copyProperties(request, inModel);
        int updateRes = businessCardVideoMapper.updateByPrimaryKeySelective(inModel);
        if (updateRes <= 0) {
            return ResultModel.fail();
        }
        return ResultModel.success();
    }

    @Override
    public ResultModel<Boolean> uodVideo(BusinessCardRequest request) {
        List<BusinessCardVideoRequest> videoRequestList=request.getVideoRequestList();
        if(null==videoRequestList || videoRequestList.size()<=0){
            log.info("视频数据为空，不进行修改名片视频操作。。。");
            return ResultModel.success();
        }
        //TODO 需要优化为批量插入
        videoRequestList.forEach(l->{{
            l.setMyCardId(request.getMyCardId());
            uodVideo(l);
        }});
        return null;
    }

    @Override
    public ResultModel<Boolean> moveVideo(BusinessCardVideoRequest request) {
        if(null==request.getSorts()){
            return ResultModel.checkParamFail();
        }
      List<BusinessCardVideoModel> list= businessCardVideoMapper.selectByCardList(new BusinessCardVideoModel(){{
            setCardId(request.getMyCardId());
        }});
        if(request.getSorts().length!=list.size()){
            return ResultModel.fail();
        }
        //TODO 需要优化成批量操作
        int i=0;
        for(BusinessCardVideoModel outModel:list){
            outModel.setSort(request.getSorts()[i++]);
            businessCardVideoMapper.updateByPrimaryKeySelective(outModel);
        }
        return ResultModel.success();
    }

    @Override
    public ResultModel<Boolean> delVideo(BusinessCardVideoRequest request) {
        if (null == request.getVideoId()) {
            return ResultModel.checkParamFail();
        }
        int delRes=businessCardVideoMapper.deleteByPrimaryKey(request.getVideoId());
        if(delRes<=0){
            return ResultModel.fail();
        }
        return ResultModel.success();
    }

    @Override
    public ResultModel<List<BusinessCardVideoResponse>> moveList(BusinessCardVideoRequest request){
        List<BusinessCardVideoModel> modelList=businessCardVideoMapper.selectByCardList(new BusinessCardVideoModel(){{
            setCardId(request.getMyCardId());
        }});
        List<BusinessCardVideoResponse> list=new ArrayList<>();
        modelList.forEach(l->{{
            BusinessCardVideoResponse response=new BusinessCardVideoResponse();
            BeanUtils.copyProperties(l,response);
            list.add(response);
        }});
        return ResultModel.success(list);
    }

}
